#import "TuyaPlugin.h"
#import <TuyaSmartHomeKit/TuyaSmartKit.h>
#import "TuyaAccount.h"
#import "TuyaHome.h"
#import "TuyaDevice.h"


@implementation TuyaPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
    FlutterMethodChannel* channel = [FlutterMethodChannel methodChannelWithName:@"tuya" binaryMessenger:[registrar messenger]];
    TuyaPlugin* instance = [[TuyaPlugin alloc] init];
    [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    // 后去设备版本号
    if ([@"getPlatformVersion" isEqualToString:call.method]) {
        result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
    }
    // 手机号码登录
    else if ([@"Login" isEqualToString:call.method]) {
        NSString *phone = @"18768177795";
        NSString *password = @"Zhou939436";
        NSString *code = @"86";
        [TuyaAccount loginByPhone:phone password:password countryCode:code result:result];
    }
    // uid 登录
    else if ([@"LoginById" isEqualToString:call.method]) {
        NSString *uid = call.arguments[@"uid"];
        NSString *password = @"Zhou939436";
        NSString *code = @"86";
        BOOL createHome = NO;
        // [TuyaAccount loginByUid:uid password:password countryCode:code result:result];
        [TuyaAccount loginByUid:uid password:password createHome:createHome countryCode:code result:result];
    }
    // 获取家庭列表
    else if ([@"getHomeList" isEqualToString:call.method]) {
        [TuyaHome getHomeList:result];
    }
    // 创建家庭
    else if ([@"createHome" isEqualToString:call.method]) {
        NSString *homeName = call.arguments[@"name"];
        NSString *geoName = @"默认位置";
        NSArray<NSString *> *rooms = @[];
        double latitude = 30;
        double longitude = 120;
        [TuyaHome addHomeWithName:homeName geoName:geoName rooms:rooms latitude:latitude longitude:longitude result:result];
    }
    // 获取家庭详情
    else if ([@"getHomeDetail" isEqualToString:call.method]) {
        long long homeId = [call.arguments[@"id"] integerValue];
        [TuyaHome getHomeDetailWithHomeId:homeId result:result];
    }
    // 开始扫描
    else if ([@"StartScanDevice" isEqualToString:call.method]) {
        [TuyaDevice startListening:result];
    }
    // 停止扫描
    else if ([@"stopScanDevice" isEqualToString:call.method]) {
        [TuyaDevice stopListening:result];
    }
    // 二维码配网
    else if ([@"activeDevice" isEqualToString:call.method]) {
        NSString *uuid = call.arguments[@"uuid"];
        long long homeId = [call.arguments[@"homeId"] integerValue];
#warning msg --- 和android 配网有点却别，这里需要添加 wifi 信息
        NSString *ssid = call.arguments[@"name"];
        NSString *password = call.arguments[@"pwd"];
        [TuyaDevice startQRCodeConfigNetWithUUID:uuid homeId:homeId ssid:ssid password:password result:result];
    }
    // 重置设备
    else if ([@"resetFactory" isEqualToString:call.method]) {
        NSString *deviceId = call.arguments[@"uuid"];
        [TuyaDevice resetFactoryWithDeviceId:deviceId result:result];
    }
    // 删除设备
    else if ([@"removeDevice" isEqualToString:call.method]) {
        NSString *deviceId = call.arguments[@"uuid"];
        [TuyaDevice removeWithDeviceId:deviceId result:result];
    }
    else {
        result(FlutterMethodNotImplemented);
    }
}

@end
